\ifx\locallinewidth\undefined\newlength{\locallinewidth}\fi
\setlength{\locallinewidth}{\linewidth}
\section{\module{elementtree} --- The xml.etree.ElementTree Module}
\declaremodule{standard}{elementtree}
\moduleauthor{Fredrik Lundh}{fredrik@pythonware.com}
\modulesynopsis{This module provides implementations of the Element and ElementTree
types, plus support classes.

A C version of this API is available as xml.etree.cElementTree.}
\versionadded{2.5}


\subsection{Overview\label{elementtree-overview}}

The Element type is a flexible container object, designed to store
hierarchical data structures in memory. The type can be described as a
cross between a list and a dictionary.

Each element has a number of properties associated with it:
\begin{itemize}
\item {} 
a tag. This is a string identifying what kind of data
this element represents (the element type, in other words).

\item {} 
a number of attributes, stored in a Python dictionary.

\item {} 
a text string.

\item {} 
an optional tail string.

\item {} 
a number of child elements, stored in a Python sequence

\end{itemize}

To create an element instance, use the Element or SubElement factory
functions.

The ElementTree class can be used to wrap an element
structure, and convert it from and to XML.


\subsection{Functions\label{elementtree-functions}}

\begin{funcdesc}{Comment}{text=None}
Comment element factory.  This factory function creates a special
element that will be serialized as an XML comment.

The comment string can be either an 8-bit ASCII string or a Unicode
string.

text: A string containing the comment string.

Returns: An element instance, representing a comment.
\end{funcdesc}

\begin{funcdesc}{dump}{elem}
Writes an element tree or element structure to sys.stdout.  This
function should be used for debugging only.

The exact output format is implementation dependent.  In this
version, it's written as an ordinary XML file.

elem: An element tree or an individual element.
\end{funcdesc}

\begin{funcdesc}{Element}{tag, attrib={}, **extra}
Inline strong start-string without end-string.


Element factory.  This function returns an object implementing the
standard Element interface.  The exact class or type of that object
is implementation dependent, but it will always be compatible with
the {\_}ElementInterface class in this module.

The element name, attribute names, and attribute values can be
either 8-bit ASCII strings or Unicode strings.

tag: The element name.

attrib: An optional dictionary, containing element attributes.

extra: Additional attributes, given as keyword arguments.

Returns: An element instance.
\end{funcdesc}

\begin{funcdesc}{fromstring}{text}
Parses an XML section from a string constant.  Same as XML.

source: A string containing XML data.

Returns: An Element instance.
\end{funcdesc}

\begin{funcdesc}{iselement}{element}
Checks if an object appears to be a valid element object.

element: element instance.

Returns: A true value if this is an element object.
\end{funcdesc}

\begin{funcdesc}{iterparse}{source, events=None}
Parses an XML section into an element tree incrementally, and reports
what's going on to the user.

source: A filename or file object containing XML data.

events: A list of events to report back.  If omitted, only ``end''
events are reported.

Returns: A (event, elem) iterator.
\end{funcdesc}

\begin{funcdesc}{parse}{source, parser=None}
Parses an XML section into an element tree.

source: A filename or file object containing XML data.

parser: An optional parser instance.  If not given, the
standard XMLTreeBuilder parser is used.

Returns: An ElementTree instance
\end{funcdesc}

\begin{funcdesc}{ProcessingInstruction}{target, text=None}
PI element factory.  This factory function creates a special element
that will be serialized as an XML processing instruction.

target: A string containing the PI target.

text: A string containing the PI contents, if any.

Returns: An element instance, representing a PI.
\end{funcdesc}

\begin{funcdesc}{SubElement}{parent, tag, attrib={}, **extra}
Inline strong start-string without end-string.


Subelement factory.  This function creates an element instance, and
appends it to an existing element.

The element name, attribute names, and attribute values can be
either 8-bit ASCII strings or Unicode strings.

parent: The parent element.

tag: The subelement name.

attrib: An optional dictionary, containing element attributes.

extra: Additional attributes, given as keyword arguments.

Returns: An element instance.
\end{funcdesc}

\begin{funcdesc}{tostring}{element, encoding=None}
Generates a string representation of an XML element, including all
subelements.

element: An Element instance.

Returns: An encoded string containing the XML data.
\end{funcdesc}

\begin{funcdesc}{XML}{text}
Parses an XML section from a string constant.  This function can
be used to embed ``XML literals'' in Python code.

source: A string containing XML data.

Returns: An Element instance.
\end{funcdesc}

\begin{funcdesc}{XMLID}{text}
Parses an XML section from a string constant, and also returns
a dictionary which maps from element id:s to elements.

source: A string containing XML data.

Returns: A tuple containing an Element instance and a dictionary.
\end{funcdesc}


\subsection{ElementTree Objects\label{elementtree-elementtree-objects}}

\begin{classdesc}{class ElementTree}{element=None, file=None}
ElementTree wrapper class.  This class represents an entire element
hierarchy, and adds some extra support for serialization to and from
standard XML.

element: Optional root element.

file (keyword): Optional file handle or name.  If given, the
tree is initialized with the contents of this XML file.
\end{classdesc}

\begin{methoddesc}{_setroot}{element}
Replaces the root element for this tree.  This discards the
current contents of the tree, and replaces it with the given
element.  Use with care.

element: An element instance.
\end{methoddesc}

\begin{methoddesc}{find}{path}
Finds the first toplevel element with given tag.
Same as getroot().find(path).

path: What element to look for.

Returns: The first matching element, or None if no element was found.
\end{methoddesc}

\begin{methoddesc}{findall}{path}
Finds all toplevel elements with the given tag.
Same as getroot().findall(path).

path: What element to look for.

Returns: A list or iterator containing all matching elements,
in section order.
\end{methoddesc}

\begin{methoddesc}{findtext}{path, default=None}
Finds the element text for the first toplevel element with given
tag.  Same as getroot().findtext(path).

path: What toplevel element to look for.

default: What to return if the element was not found.

Returns: The text content of the first matching element, or the
default value no element was found.  Note that if the element
has is found, but has no text content, this method returns an
empty string.
\end{methoddesc}

\begin{methoddesc}{getiterator}{tag=None}
Creates a tree iterator for the root element.  The iterator loops
over all elements in this tree, in section order.

tag: What tags to look for (default is to return all elements)

Returns: An iterator.
\end{methoddesc}

\begin{methoddesc}{getroot}{}
Gets the root element for this tree.

\begin{datadescni}{Returns:}
An element instance.
\end{datadescni}
\end{methoddesc}

\begin{methoddesc}{parse}{source, parser=None}
Loads an external XML section into this element tree.

source: A file name or file object.

parser: An optional parser instance.  If not given, the
standard XMLTreeBuilder parser is used.

Returns: The section root element.
\end{methoddesc}

\begin{methoddesc}{write}{file, encoding="us-ascii"}
Writes the element tree to a file, as XML.

file: A file name, or a file object opened for writing.

encoding: Optional output encoding (default is US-ASCII).
\end{methoddesc}


\subsection{QName Objects\label{elementtree-qname-objects}}

\begin{classdesc}{class QName}{text_or_uri, tag=None}
QName wrapper.  This can be used to wrap a QName attribute value, in
order to get proper namespace handling on output.

text: A string containing the QName value, in the form {\{}uri{\}}local,
or, if the tag argument is given, the URI part of a QName.

tag: Optional tag.  If given, the first argument is interpreted as
an URI, and this argument is interpreted as a local name.

Returns: An opaque object, representing the QName.
\end{classdesc}


\subsection{TreeBuilder Objects\label{elementtree-treebuilder-objects}}

\begin{classdesc}{class TreeBuilder}{element_factory=None}
Generic element structure builder.  This builder converts a sequence
of start, data, and end method calls to a well-formed element structure.

You can use this class to build an element structure using a custom XML
parser, or a parser for some other XML-like format.

element{\_}factory: Optional element factory.  This factory
is called to create new Element instances, as necessary.
\end{classdesc}

\begin{methoddesc}{close}{}
Flushes the parser buffers, and returns the toplevel documen
element.

\begin{datadescni}{Returns:}
An Element instance.
\end{datadescni}
\end{methoddesc}

\begin{methoddesc}{data}{data}
Adds text to the current element.

data: A string.  This should be either an 8-bit string
containing ASCII text, or a Unicode string.
\end{methoddesc}

\begin{methoddesc}{end}{tag}
Closes the current element.

tag: The element name.

Returns: The closed element.
\end{methoddesc}

\begin{methoddesc}{start}{tag, attrs}
Opens a new element.

tag: The element name.

attrib: A dictionary containing element attributes.

Returns: The opened element.
\end{methoddesc}


\subsection{XMLTreeBuilder Objects\label{elementtree-xmltreebuilder-objects}}

\begin{classdesc}{class XMLTreeBuilder}{html=0, target=None}
Element structure builder for XML source data, based on the
expat parser.

target (keyword): Target object.  If omitted, the builder uses an
instance of the standard TreeBuilder class.

html (keyword): Predefine HTML entities.  This flag is not supported
by the current implementation.
\end{classdesc}

\begin{methoddesc}{close}{}
Finishes feeding data to the parser.

\begin{datadescni}{Returns:}
An element structure.
\end{datadescni}
\end{methoddesc}

\begin{methoddesc}{doctype}{name, pubid, system}
Handles a doctype declaration.

name: Doctype name.

pubid: Public identifier.

system: System identifier.
\end{methoddesc}

\begin{methoddesc}{feed}{data}
Feeds data to the parser.

data: Encoded data.
\end{methoddesc}

